package com.ahammertest.ycgk.jpush;

import android.app.Notification;
import android.content.Context;
import android.text.TextUtils;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;

import java.util.Set;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by jackmar.
 * For：极光推送工具类
 */

public class JPushHelper {
    private static JPushHelper singleton;
    Context context;

    private JPushHelper() {

    }

    public static JPushHelper getInstance() {
        if (singleton == null) {
            synchronized (JPushHelper.class) {
                if (singleton == null) {
                    singleton = new JPushHelper();
                }
            }
        }
        return singleton;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public void init(Context context) {
        JPushInterface.init(context);
        this.context = context;
    }

    /**
     * 设置别名
     *
     * @param context
     * @param alias
     */
    public void setAlias(Context context, String alias) {
        String preAlias = SPUtils.getPrefString(context, "alias", "");
//        String preAlias = PreHelper.defaultCenter(context).getStringData(Constant.PRE.PushRegistState);
        if (TextUtils.isEmpty(preAlias) && !TextUtils.isEmpty(alias) && !preAlias.equals(alias)) {
            JPushInterface.setAlias(context, alias, mTagAliasCallback);
        }

    }


    //极光推送设置别名和标签回调
    private TagAliasCallback mTagAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int i, String s, Set<String> set) {
            if (i == 0) {
//                PreHelper.defaultCenter(ActivityManager.getCurrentActiveActivity()).setData(Constant.PRE.PushRegistState, s);
                SPUtils.setPrefString(context, "alias", s);
                setNotification();
            }
        }
    };

    /**
     * 设置通知
     */
    private void setNotification() {
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(context);
        builder.statusBarDrawable = R.mipmap.logo_sque;
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
        builder.notificationDefaults = Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
        JPushInterface.setPushNotificationBuilder(1, builder);

    }
}
